/*
* @Author: baidwwy
* @Date:   2019-04-08 05:41:45
* @Last Modified by:   baidwwy
* @Last Modified time: 2020-05-17 15:50:36
*/
#include "gge.h"
#include "SDL_keyboard.h"

static int	LUA_GetKeyFromScancode(lua_State *L)
{
	SDL_Scancode sc = (SDL_Scancode)luaL_checkinteger(L, 1);
	lua_pushinteger(L,SDL_GetKeyFromScancode(sc));
	return 1;
}

static int	LUA_GetScancodeFromKey(lua_State *L)
{
	SDL_Keycode kc = (SDL_Keycode)luaL_checkinteger(L, 1);
	lua_pushinteger(L,SDL_GetScancodeFromKey(kc));
	return 1;
}

static int	LUA_GetScancodeName(lua_State *L)
{
	SDL_Scancode code = (SDL_Scancode)luaL_checkinteger(L, 1);
	lua_pushstring(L,SDL_GetScancodeName(code));
	return 1;
}

static int	LUA_GetScancodeFromName(lua_State *L)
{
	const char *name = luaL_checkstring(L, 1);
	lua_pushinteger(L,SDL_GetScancodeFromName(name));
	return 1;
}

static int	LUA_GetKeyName(lua_State *L)
{
	SDL_Keycode kc = (SDL_Keycode)luaL_checkinteger(L, 1);
	lua_pushstring(L,SDL_GetKeyName(kc));
	return 1;
}

static int	LUA_GetKeyFromName(lua_State *L)
{
	const char *name = luaL_checkstring(L, 1);
	lua_pushinteger(L,SDL_GetKeyFromName(name));
	return 1;
}
static int	LUA_StartTextInput(lua_State *L)
{
	SDL_StartTextInput();
	return 0;
}
static int	LUA_IsTextInputActive(lua_State *L)
{
	lua_pushboolean(L,SDL_IsTextInputActive());
	return 1;
}
static int	LUA_StopTextInput(lua_State *L)
{
	SDL_StopTextInput();
	return 0;
}
static int	LUA_SetTextInputRect(lua_State *L)
{
	SDL_Rect * rect = (SDL_Rect*)luaL_checkudata(L, 1, "SDL_Rect");

	SDL_SetTextInputRect(rect);
	return 0;
}
static int	LUA_HasScreenKeyboardSupport(lua_State *L)
{
	lua_pushboolean(L,SDL_HasScreenKeyboardSupport());
	return 1;
}
static int	LUA_IsScreenKeyboardShown(lua_State *L)
{
	SDL_Window * win = (SDL_Window*)luaL_checkudata(L, 1, "SDL_Window");

	lua_pushboolean(L,SDL_IsScreenKeyboardShown(win));
	return 0;
}

static const luaL_Reg SDL_Meta[] = {
	{"GetKeyFromName"             , LUA_GetKeyFromName}           ,
	{"GetKeyFromScancode"         , LUA_GetKeyFromScancode}       ,
	{"GetKeyName"                 , LUA_GetKeyName}               ,
	{"GetScancodeFromKey"         , LUA_GetScancodeFromKey}       ,
	{"GetScancodeFromName"        , LUA_GetScancodeFromName}      ,
	{"GetScancodeName"            , LUA_GetScancodeName}          ,
	//{"HasScreenKeyboardSupport" , LUA_HasScreenKeyboardSupport} ,
	//{"IsScreenKeyboardShown"    , LUA_IsScreenKeyboardShown}    ,
	{"IsTextInputActive"        , LUA_IsTextInputActive}        ,

	{"SetTextInputRect"           , LUA_SetTextInputRect}         ,
	{"StartTextInput"             , LUA_StartTextInput}           ,
	{"StopTextInput"              , LUA_StopTextInput}            ,
	{ NULL,	NULL}
};

void OPEN_Keyboard(lua_State *L)
{

	luaL_setfuncs(L,SDL_Meta,0);
}
